from django.db import models
from django.contrib.auth import get_user_model
from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField

User = get_user_model()


class Criteria(models.Model):
    name = models.CharField(max_length=30, verbose_name="标准名称")
    code = models.CharField(max_length=30, null=True, blank=True,verbose_name="标准号")
    pub_date = models.DateField(null=True, blank=True, verbose_name="发布日期")
    effective_date = models.DateField(null=True, blank=True, verbose_name="生效日期")

    class Meta:
        verbose_name = "评审标准"
        verbose_name_plural = verbose_name
        ordering=['id']

    def __str__(self):
        return self.name


class ReviewClause(models.Model):
    """
    评审条款
    """

    level_choices = (("1", "一级目录"), ("2", "二级目录"), ("3", "三级目录"), ("4", "四级目录"), ("6", "叶子条款"))
    book_choices = ((1, "三级综合医院评审标准(2011年版)"), (2, "三级综合医院评审标准(2013年版)"))
    criteria = models.ForeignKey(Criteria, on_delete=models.SET_NULL, null=True, blank=True, verbose_name="所属标准")
    parent = models.ForeignKey("self", on_delete=models.SET_NULL,null=True, blank=True,  verbose_name="父条款")
    code = models.CharField(max_length=30,null=True, blank=True, verbose_name="条款代码")
    name = models.CharField(max_length=30, verbose_name="条款名称", help_text="条款名称")
    level = models.CharField(max_length=20, choices=level_choices, default="1", verbose_name="条款级别")
    sort = models.IntegerField(default=1, verbose_name="排序")
    desc = models.TextField(blank=True, null=True, verbose_name="条款内容")
    iscore = models.BooleanField(default=0, verbose_name="是否核心条款")
    isopen = models.BooleanField(default=1, verbose_name="是否开放")
     # = models.CharField(max_length=20, choices=book_choices, default=1, verbose_name="目录级别")

    # oldid = models.IntegerField(null=True, blank=True, verbose_name="oldid")
    # oldpid = models.IntegerField(null=True, blank=True, verbose_name="oldpid")

    class Meta:
        verbose_name = "评审条款"
        verbose_name_plural = verbose_name
        # app_label = 'criterion'

    def __str__(self):
        return self.name


class ReviewKey(models.Model):
    """
    评审要点
    """
    level_choices = (("A", "A"), ("B", "B"), ("C", "C"), ("D", "D"))
    code = models.CharField(max_length=30,null=True, blank=True, verbose_name="要点代码")
    name = models.CharField(max_length=30, verbose_name="要点名称", help_text="要点名称")
    level = models.CharField(max_length=20, choices=level_choices, default="D", verbose_name="等级")
    sort = models.IntegerField(default=1, verbose_name="排序")
    desc = models.TextField(blank=True, null=True, verbose_name="要点内容")
    clause = models.ForeignKey("ReviewClause", on_delete=models.SET_NULL, limit_choices_to={'level': 6}, null=True, blank=True, verbose_name="所属条款")
    iscore = models.BooleanField(default=0, verbose_name="是否核心要点")
    document = models.TextField(blank=True, null=True, verbose_name="查阅资料")
    interview = models.TextField(blank=True, null=True, verbose_name="访谈与追踪")
    inspect = models.TextField(blank=True, null=True, verbose_name="实地查看与考核")

    # oldid = models.IntegerField(null=True, blank=True, verbose_name="oldid")
    # oldpid = models.IntegerField(null=True, blank=True, verbose_name="oldpid")

    class Meta:
        verbose_name = "评审要点"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.desc

class ReviewReference(models.Model):
    """
    评审参考资料
     """
    type_choices = (("A", "材料目录"), ("B", "模板列表"), ("C", "现场要点"), ("D", "法律法规"))
    # code = models.CharField(max_length=30, null=True, blank=True,verbose_name="资料代码")
    # name = models.CharField(max_length=30, null=True, blank=True,verbose_name="资料名称")

    clause = models.ForeignKey("ReviewClause", on_delete=models.SET_NULL, limit_choices_to={'level': 6}, null=True, blank=True, verbose_name="所属条款")
    type = models.CharField(max_length=20, choices=type_choices, default="D", verbose_name="资料类型")
    # sort = models.IntegerField(default=1, verbose_name="排序")
    desc = RichTextUploadingField(verbose_name="资料内容")

    class Meta:
        verbose_name = "参考资料"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.desc


class RefFile(models.Model):
    """
    参考资料文件
    """
    clause = models.ForeignKey("ReviewClause", on_delete=models.SET_NULL, limit_choices_to={'level': 6}, null=True, blank=True, verbose_name="所属条款")
    sort = models.IntegerField(default=1, verbose_name="文件排序")
    name = models.CharField(max_length=30, verbose_name="文件名称")
    file = models.FileField(upload_to='task/%Y/%m', blank=True, null=True, verbose_name='参考资料')
    operator = models.ForeignKey(User,blank=True, null=True, on_delete=models.SET_NULL, verbose_name="操作人")
    optime = models.DateTimeField(auto_now=True, verbose_name="上传时间")

    class Meta:
        verbose_name = "参考资料文件"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
